import asyncio
import requests
import json

class Gemini_API_Ddemo:

    def __init__(self) -> None:
        self.audio_in_queue = asyncio.Queue()
        self.audio_out_queue = asyncio.Queue()
        self.video_out_queue = asyncio.Queue()
        pass

    async def run(self, api_key):
        url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
        headers = {
            "Content-Type": "application/json"
        }
        data = {
            "contents": [
                {
                    "parts": [
                        {
                            "text": "Explain how AI works"
                        }
                    ]
                }
            ]
        }
        
        response = requests.post(url, headers=headers, params={"key": api_key}, data=json.dumps(data))
        
        if response.status_code == 200:
            json_response = response.json()
            candidates = json_response.get('candidates', [])
            
            if candidates:
                text_content = candidates[0]['content']['parts'][0]['text']
                print("AI Response:\n", text_content)  # 只打印文本内容
            else:
                print("No candidates found in the response.")
        else:
            print("Error:", response.status_code, response.text)

if __name__ == "__main__":
    YOUR_API_KEY = "AIzaSyCLr_J6mqzmydsE1iBMI1erFBmdmGlZ2zA"  # 替换为你的API密钥

    main = Gemini_API_Ddemo()
    asyncio.run(main.run(YOUR_API_KEY))
